class Solution(object):
    def robotSim(self, commands, obstacles):
        ob = {(x, y) for x, y in obstacles}
        max_value = 0
        point = (0, 0)
        state = 0
        for c in commands:
            if c < 0:
                if c == -1:
                    state = (state + 1) % 4
                if c == -2:
                    state = (state - 1) % 4
            else:
                while c > 0:
                    if state == 0:
                        next_point = (point[0], point[1] + 1)
                    if state == 1:
                        next_point = (point[0] + 1, point[1])
                    if state == 2:
                        next_point = (point[0], point[1] - 1)
                    if state == 3:
                        next_point = (point[0] - 1, point[1])
                    if next_point not in ob:
                        point = next_point
                        c -= 1
                    else:
                        break
                max_value = max(max_value, point[0] ** 2 + point[1] ** 2)
        return max_value




data = Solution()
commands = [4, -1, 4, -2, 4]
obstacles = [[2, 4]]
print(data.robotSim(commands, obstacles))
